package com.fish.admin.module.salesman.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fish.admin.constant.SalesmanConstants;
import com.fish.admin.module.auth.domain.entity.AuthInfo;
import com.fish.admin.module.auth.manager.AuthInfoManager;
import com.fish.admin.module.salesman.domain.bo.SalesmanInfoBO;
import com.fish.admin.module.salesman.domain.dto.SalesmanInfoDTO;
import com.fish.admin.module.salesman.domain.dto.SalesmanInfoPageQueryDTO;
import com.fish.admin.module.salesman.domain.entity.SalesmanInfo;
import com.fish.admin.module.salesman.domain.vo.SalesmanInfoVO;
import com.fish.admin.module.salesman.enums.AuthStatusEnum;
import com.fish.admin.module.salesman.manager.SalesmanInfoManager;
import com.fish.admin.module.salesman.service.SalesmanInfoService;
import com.fish.admin.util.UniqueIDGenerator;
import com.fish.common.core.base.LoginAppUser;
import com.fish.common.core.context.UserContextManager;
import com.fish.common.core.domain.vo.PageVO;
import com.fish.common.core.enums.EnabledEnum;
import com.fish.common.core.enums.ReadonlyEnum;
import com.fish.common.core.enums.StatusEnum;
import com.fish.common.core.enums.UserTypeEnum;
import com.fish.common.mybatisplus.util.PageUtils;
import com.fish.common.support.util.AssertUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class SalesmanInfoImpl implements SalesmanInfoService {

    private final SalesmanInfoManager salesmanInfoManager;

    private final AuthInfoManager authInfoManager;

    @Override
    public PageVO<SalesmanInfoVO> page(SalesmanInfoPageQueryDTO salesmanInfoPageQueryDTO) {

        IPage<SalesmanInfoBO> page =salesmanInfoManager.page(salesmanInfoPageQueryDTO);
        return PageUtils.toPage(page, (e -> {
            SalesmanInfoVO v = new SalesmanInfoVO();
            BeanUtils.copyProperties(e, v);
            v.setInviteLink("https://localhost:8080/?code="+e.getSalesmanNo());
            return v;
        }));
    }

    @Override
    @Transactional(rollbackFor =Exception.class)
    public void save(SalesmanInfoDTO salesmanInfoDTO) {
        SalesmanInfo salesmanInfo = new SalesmanInfo();
        salesmanInfo.setRoleId(salesmanInfoDTO.getRoleId());
        salesmanInfo.setAccount(salesmanInfoDTO.getAccount());
        LoginAppUser loginAppUser = UserContextManager.getRequiredContext();
        AssertUtils.isFalse(loginAppUser.isAdmin(),"系统用户不能直接添加业务员");
        salesmanInfo.setSalesmanNo(UniqueIDGenerator.generateUniqueIDs());
        salesmanInfo.setAuthStatus(AuthStatusEnum.YES.getValue());
        salesmanInfo.setEnabled(EnabledEnum.YES.getValue());
        salesmanInfo.setStatus(StatusEnum.YES.getValue());
        salesmanInfo.setGameQuota(SalesmanConstants.DEFAULT_GAME_QUOTA);
        salesmanInfo.setUsedGameQuota(SalesmanConstants.DEFAULT_USED_GAME_QUOTA);
        salesmanInfo.setLoanQuota(SalesmanConstants.DEFAULT_LOAN_QUOTA);
        salesmanInfo.setUsedLoanQuota(SalesmanConstants.DEFAULT_USED_LOAN_QUOTA);
        salesmanInfo.setTenantNo(loginAppUser.getTenantNo());
        if(loginAppUser.isTenant()) {
            salesmanInfo.setDataScope(salesmanInfoDTO.getDataScope());
            salesmanInfo.setDataPath(salesmanInfo.getSalesmanNo());
            salesmanInfo.setUserLevel(1);
        }else if (loginAppUser.isSalesman()){
            SalesmanInfo parent = salesmanInfoManager.getById(loginAppUser.getUserId());
            AssertUtils.notNull(parent, "上级业务员不存在");
            salesmanInfo.setParentId(parent.getId());
            salesmanInfo.setDataScope(parent.getDataScope());
            salesmanInfo.setDataPath("-"+parent.getDataPath()+salesmanInfo.getSalesmanNo());
            salesmanInfo.setUserLevel(parent.getUserLevel()+1);
        }
        salesmanInfoManager.save(salesmanInfo);
        AuthInfo authInfo = new AuthInfo();
        authInfo.setAccount(salesmanInfoDTO.getAccount());
        authInfo.setSecret(salesmanInfoDTO.getSecret());
        authInfo.setUserId(salesmanInfo.getId());
        authInfo.setUserType(UserTypeEnum.SALESMAN.getValue());
        authInfo.setEnabled(EnabledEnum.YES.getValue());
        authInfo.setStatus(StatusEnum.YES.getValue());
        authInfo.setReadonly(ReadonlyEnum.YES.getValue());
        authInfo.setDataPath(salesmanInfo.getDataPath());
        authInfoManager.save(authInfo);


    }

    @Override
    public SalesmanInfo getById(Long id) {
        return salesmanInfoManager.getById(id);
    }
}
